package cn.fintecher.pangolin.service.dataimp.request;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 查询对账单导入详情请求参数
 * @Date: 17:35 2019/10/8
 */

@Data
public class FindBillRequest {
    @ApiModelProperty(value = "批次号")
    private String batchNumber;

    @ApiModelProperty(value = "案件编号")
    private String caseNumber;

    @ApiModelProperty(value = "最小还款日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date minPaymentDate;

    @ApiModelProperty(value = "最大还款日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date maxPaymentDate;

    @ApiModelProperty(value = "最小还款金额")
    private Double minPaymentAmt;

    @ApiModelProperty(value = "最大还款金额")
    private Double maxPaymentAmt;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("batchNumber.keyword", this.batchNumber));
        builder.must(QueryBuilders.termQuery("flag", 0));
        if (StringUtils.isNotBlank(this.caseNumber)) {
            builder.must(QueryBuilders.wildcardQuery("caseNumber.keyword", this.caseNumber));
        }
        if (Objects.nonNull(this.minPaymentDate)) {
            builder.must(QueryBuilders.rangeQuery("paymentDate").gte(this.minPaymentDate.getTime()));
        }
        if (Objects.nonNull(this.maxPaymentDate)) {
            builder.must(QueryBuilders.rangeQuery("paymentDate").lte(this.maxPaymentDate.getTime() + 86400000));
        }
        if (Objects.nonNull(this.minPaymentAmt)) {
            builder.must(QueryBuilders.rangeQuery("paymentAmt").gte(this.minPaymentAmt));
        }
        if (Objects.nonNull(this.maxPaymentAmt)) {
            builder.must(QueryBuilders.rangeQuery("paymentAmt").lte(this.maxPaymentAmt));
        }
        return builder;
    }
}
